from flask import Blueprint, jsonify, request
from app.services.arm_service import ArmService
import json
        
def init_arm_routes(ros_utils):
    arm_bp = Blueprint('arm', __name__)
    arm_service = ArmService(ros_utils)
    
    @arm_bp.route('/process', methods=['GET'])  # 完整路径: /api/arm/process
    def arm_process():
        result = arm_service.process()
        return jsonify(result), result["code"]
    
    @arm_bp.route('/clearError', methods=['GET'])  # 完整路径: /api/arm/clearError
    def arm_clear_error():
        result = arm_service.clear_error()
        return jsonify(result), result["code"]
    
    @arm_bp.route('/goHome', methods=['GET'])
    def arm_go_home():
        result = arm_service.go_home("0")
        return jsonify(result), result["code"]
    
    @arm_bp.route('/readCal', methods=['GET'])
    def arm_read_cal():
        result = arm_service.read_cal(" ")
        return jsonify(result), result["code"]
    
    @arm_bp.route('/isShot', methods=['POST'])
    def arm_setshot():
        data = request.get_json()
        if not data:
            return jsonify({
                "code": 400,
                "message": "缺少参数",
                "data": None
            }), 200
        
        status = data['status']
        result = arm_service.set_shot(status)
        return jsonify(result), result["code"]
    @arm_bp.route('/isDown', methods=['POST'])
    def arm_setdown():
        data = request.get_json()
        if not data:
            return jsonify({
                "code": 400,
                "message": "缺少参数",
                "data": None
            }), 200
        
        status = data['status']
        result = arm_service.set_down(status)
        return jsonify(result), result["code"]
    
    @arm_bp.route('/emergency', methods=['POST'])
    def arm_emergency():
        data = request.get_json()
        if not data:
            return jsonify({
                "code": 400,
                "message": "缺少参数",
                "data": None
            }), 200
        
        status = data['status']
        result = arm_service.emergency(status)
        return jsonify(result), result["code"]
    
    @arm_bp.route('/alertCal', methods=['POST'])
    def arm_alert_cal():
        data = request.get_json()
        if not data:
            return jsonify({
                "code": 400,
                "message": "缺少参数",
                "data": None
            }), 200
        args_json = json.dumps(data)
        result = arm_service.alert_cal(args_json)
        return jsonify(result), result["code"]
    return arm_bp